package other;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

public class PolandCalculator {
    static final Map<String,Integer> opPriorityMap;
    static {
        opPriorityMap = new HashMap<>();
        opPriorityMap.put("+",1);
        opPriorityMap.put("-",1);
        opPriorityMap.put("*",2);
        opPriorityMap.put("/",2);
    }
    public static void main(String[] args) {
        String expression="( 30 + 4 ) * 5 - 6";
        //1+((2+3)*4)-5   ( 30 + 4 ) * 5 - 6
        String pex = convertEx(expression);
        System.out.println(pex);
        int result = calc(pex);
        System.out.println(result);
    }

    //将中缀表达式转换为后缀表达式
    private static String convertEx(String expression) {
        String[] strings = expression.split(" ");
        Stack<String> opStack = new Stack<>();
        Stack<String> numStack = new Stack<>();
        for(int i=0;i<strings.length;i++){
            String str = strings[i];
           if(str.matches("\\d+")){
               numStack.push(str);
           }else if (opPriorityMap.get(str)!=null){
               int priority = opPriorityMap.get(str);
               System.out.println(opStack);
               while(true){
                   if(opStack.empty()){
                       opStack.push(str);
                       break;
                   }

                   if("(".equals(opStack.peek())){
                       opStack.push(str);
                       break;
                   }

                   if(opPriorityMap.get(opStack.peek())<=priority){
                       opStack.push(str);
                       break;
                   }
                   numStack.push(opStack.pop());
               }
           }else if ("(".equals(str)){
               opStack.push(str);
           }else if (")".equals(str)){
               while(!(str=opStack.pop()).equals("(")){
                   if(opStack.empty()){
                       throw new RuntimeException("括号不成对");
                   }
                   numStack.push(str);
               }
           }else {
               throw new RuntimeException("运算符错误");
           }
        }

        while(!opStack.empty()){
            numStack.push(opStack.pop());
        }

        String result = "";
        while(!numStack.empty()){
            result=numStack.pop() + " " +result;
        }
        return result;
    }

    private static int calc(String expression) {
        String[] strings = expression.split(" ");
        Stack<String> stack = new Stack<>();
        for(int i=0;i<strings.length;i++){
            String str = strings[i];
            if(str.matches("\\d+")){
                stack.push(str);
                continue;
            }

            int num2 = Integer.parseInt(stack.pop());
            int num1 = Integer.parseInt(stack.pop());
            int result=0;
            if(str.equals("+")){
                result = num1+num2;
            }else if (str.equals("-")){
                result = num1-num2;
            }else if (str.equals("*")){
                result = num1*num2;
            }else if (str.equals("/")){
                result = num1/num2;
            }else {
                throw new RuntimeException("运算符错误");
            }
            stack.push(result+"");
        }
        return Integer.parseInt(stack.pop());
    }

}
